package com.wiz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wiz.mapper.CardTockMapper;
import com.wiz.service.CardTockService;
import com.wiz.to.CardTockTO;
import com.wiz.vo.criteria.CardTockSearchCriteriaTO;
//@Transactional
@Service("cardTockService")
public class CardTockServiceImpl implements CardTockService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private CardTockMapper cardTockMapper;

	@Override
	public List<CardTockTO> getCardTocks(CardTockSearchCriteriaTO searchCriteria) {
		return cardTockMapper.getCardTocks(searchCriteria);
	}

	@Override
	public int getCardTockCnt(CardTockSearchCriteriaTO searchCriteria) {
		return cardTockMapper.getCardTockCnt(searchCriteria);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public int addbatch(List<List<String>> dataList)throws Exception {
		List<CardTockTO>cards = new ArrayList<CardTockTO>();
		String cardNo= null; 
		String phyCardNo= null;
		String cardType = null;
		String hqId = null;
		String hqName = null;
		CardTockTO cardTockTO = null;
		int size = 0;
		for (List<String> list : dataList) {
			size = list.size();
			if(size>=1) phyCardNo = list.get(0);
			if(size>=2)cardNo =list.get(1);
			if(size>=3)cardType =list.get(2);
			if(size>=4)hqId =list.get(3);
			if(size>=5)hqName =list.get(4);
			if(cardNo == null ||cardNo.equals("")||phyCardNo == null
					||phyCardNo.equals("")||cardType == null ||cardType.equals("")){
				throw new Exception("逻辑卡号、物理卡号、卡类型都不能为空！");
			}
			if(!cardType.equals("1")&&!cardType.equals("2")){
				throw new Exception("卡类型值：1会员卡，2员工卡！");
			}
			//测试，正式时修改检验码
			cardTockTO = new CardTockTO(hqId,hqName,cardNo, phyCardNo, "123456789",
					Integer.valueOf(cardType),new Date());
			cards.add(cardTockTO);
		}
		return cardTockMapper.addbatch(cards);
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public int batchUpdate(List<List<String>> dataList)throws Exception {
//		List<CardTockTO>cards = new ArrayList<CardTockTO>();
		String cardNo= null; 
		String hqId= null;
		String hqName= null;
		int size = 0;
		CardTockTO cardTockTO = null;
		for (List<String> list : dataList) {
			size = list.size();
			if(size>=1)cardNo = list.get(0);
			if(size>=2)hqId=list.get(1);
			if(size>=3)hqName=list.get(2);
			if(cardNo == null ||cardNo.equals("")||hqId == null
					||hqId.equals("")||hqName == null
							||hqName.equals("")){
				throw new Exception("逻辑卡号、总部编码、总部名称都不能为空！");
			}
			cardTockTO = new CardTockTO(hqId,hqName,cardNo, null, "",
					0,null);
//			cards.add(cardTockTO);
			cardTockMapper.batchUpdate(cardTockTO);
		}
		return 0;
	}

	

	
}